/*
 * LabelComboBox 带文字的下拉菜单
 */
import QtQuick 2.14
import QtQuick.Layouts 1.14
import QtQuick.Controls 2.14

Item {
    id: root

    /* property 定义开始 */
    // combobox
    property string boxBorderColor: "#FFBBBBBB"
    // 标签排列方式
    property int labelPosition: GLOBAL.LabelPosition.Left // 默认左右布局
    property int comboBoxWidth: 273
    property int comboBoxHeight: 42
    property int comboBoxRadius: 4
    property int showListMaxCountinPage: 3 // 一个页面要展示的最大Item数
    property var model: []
    property string comboDefaultText: "" // 当model为空时，combobox上显示的默认文字

    // 文字
    property string boxFontFamily: "Alibaba PuHuiTi"
    property int boxFontPixelSize: 17
    property string boxFontColor: "#FF222222"
    property int boxFontHorAlign: Text.AlignRight
    property string comboBoxText: "--ComboBox Default--"

    // 标签宽高
    property int comboBoxLabelWidth: 0
    property int comboBoxLabelHeight: 0

    // 布局
    property int gridRowCount: _getGirdRowNCol(labelPosition)[0]  // 网格布局行数
    property int gridColCount: _getGirdRowNCol(labelPosition)[1] // 网格布局列数
    property int rowSpacing: 0
    property int colSpacing: 8

    // 私有
    property int _setComboBoxLabelWidth: -1
    property int _setComboBoxLabelHeight: -1

    // 信号
    signal currentIndexChanged(int index) // 当前下标
    signal currentTextChanged(string text) // 当前文本
    signal currentValueChanged(var value) // 当前值

    // 函数
    /*
     * 获取网格布局行列数
     * @param {int} labelPosition 布局方式
     * @return {list<int, int>} 网格布局行列数: [row, col]
     */
    property var _getGirdRowNCol: function(labelPosition){
        // 左右布局
        if(labelPosition === GLOBAL.LabelPosition.Left){
            return [1, 2]
        }
        // 上下布局
        else if(labelPosition === GLOBAL.LabelPosition.Top){
            return [2, 1]
        }
    }

    /*
     * 获取标签宽高
     * @return {Array} 标签宽高
     */
    function getLabelSize(){
        return [comboBoxLabelWidth, comboBoxLabelHeight]
    }

    /*
     * 设置标签宽高
     * @param {int} width 标签宽度
     * @param {int} height 标签高度
     */
    function setLabelSize(width, height){
        if(width){
            _setComboBoxLabelWidth = width
            label.width = _setComboBoxLabelWidth
        }
        if(height){
            _setComboBoxLabelHeight = height
            label.height = _setComboBoxLabelHeight
        }
    }

    /* property 定义结束 */

    Item {
        id: container
        anchors.fill: parent

        GridLayout {
            anchors.fill: parent
            // spacing: 8
            rows: gridRowCount
            columns: gridColCount
            rowSpacing: rowSpacing
            columnSpacing: colSpacing
            Layout.margins: 0

            // 文字
            Label {
                id: label
                font.family: boxFontFamily
                font.pixelSize: boxFontPixelSize
                color: boxFontColor
                text: comboBoxText
                horizontalAlignment: boxFontHorAlign
                verticalAlignment: Text.AlignVCenter

                onWidthChanged: {
                    comboBoxLabelWidth = width
                }

                onHeightChanged: {
                    comboBoxLabelHeight = height
                }
            }

            // combobox
            CustomComboBox {
                Layout.preferredWidth: comboBoxWidth
                Layout.fillWidth: true
                Layout.preferredHeight: comboBoxHeight
                comboFontPixelSize: boxFontPixelSize
                comboBorderColor: boxBorderColor
                showListMaxCountinPage: root.showListMaxCountinPage
                model: root.model
                comboDefaultText: root.comboDefaultText

                onCurrentIndex: root.currentIndexChanged(index)
                onCurrentText: root.currentIndexChanged(text)
                onCurrentValue: root.currentIndexChanged(value)
            }
        }
    }
}
